import {ref} from "vue";

const homeInfo = JSON.parse(sessionStorage.getItem("homeInfo"));

// 可提前签到/延后签退的分钟数（从 homeInfo 读取）
const checkBeforeAfterTime = homeInfo["checkBeforeAfterTime"] || 10;

/**
 * 是否可以签到（默认预约时间内 + 可提前分钟数）
 */
export function canCheckInNow(data) {
    const now = new Date();
    const startDateTime = new Date(`${data['reservationDate']}T${data.startTime}`);
    const endDateTime = new Date(`${data['reservationDate']}T${data.endTime}`);

    const checkInStartTime = new Date(startDateTime.getTime() - checkBeforeAfterTime * 60000);
    const checkInEndTime = endDateTime;

    return now >= checkInStartTime && now <= checkInEndTime;
}

/**
 * 是否可以签退（默认预约时间内 + 可延后分钟数）
 */
export function canCheckOutNow(data) {
    const now = new Date();
    const startDateTime = new Date(`${data['reservationDate']}T${data.startTime}`);
    const endDateTime = new Date(`${data['reservationDate']}T${data.endTime}`);

    const checkOutStartTime = startDateTime;
    const checkOutEndTime = new Date(endDateTime.getTime() + checkBeforeAfterTime * 60000);
    //console.log(checkOutStartTime + " " + checkOutEndTime)
    return now >= checkOutStartTime && now <= checkOutEndTime;
}
export function isStartBefore(data){
    // 拼接预约日期和开始时间成完整的时间戳
    const reservationStart = new Date(`${data['reservationDate']}T${data.startTime}`);
    const now = new Date();
    return now < reservationStart;

}

export function isNow(data){
    const now = new Date();
    // 生成当日的完整时间戳
    const startDateTime = new Date(`${data['reservationDate']}T${data.startTime}`);
    const endDateTime = new Date(`${data['reservationDate']}T${data.endTime}`);

    return now >= startDateTime && now <= endDateTime;
}

export function makeStatus(data) {
    const now = new Date();
    const startDateTime = new Date(`${data['reservationDate']}T${data.startTime}`);
    const endDateTime = new Date(`${data['reservationDate']}T${data.endTime}`);
    if (data["clockOut"]){
        return getStatus('#909399', "已签退");
    }
    if (data.status === "APPROVED") {
        if (now < startDateTime) {
            return getStatus('#409EFF', "未开始");
        } else if (now >= startDateTime && now <= endDateTime) {
            return getStatus('#46afff', "进行中");
        } else {
            return getStatus('#909399', "已结束");
        }
    }

    if (data.status === "PENDING") {
        return getStatus('#FFA500', "审核中");
    } else if (data.status === "CANCELLED") {
        return getStatus('#909399', "已取消");
    } else if (data.status === "REJECTED") {
        return getStatus('red', "未通过");
    }

    return getStatus('#909399', "未知状态"); // fallback
}

export function makeLoanStatus(data){
    // PENDING_REVIEW("审核中"),
    //     REJECTED("审核被拒绝"),
    //     APPROVED("审核通过"),
    //     RETURNED("已归还");


    if (data.status === "PENDING_REVIEW"){
        return getStatus('#FFA500', "审核中")
    } else if(data.status === "APPROVED"){
        return getStatus('green',"已通过")
    } else if(data.status === "CANCELLED"){
        return getStatus('#909399',"已取消")
    } else if(data.status === "REJECTED"){
        return getStatus('red',"未通过")
    } else if(data.status === "RETURNED"){
        return getStatus('#46afff', "已归还");
    }

}

function getStatus(color, text){
    return ref({
        color: color,
        text: text
    })
}


